'use strict';

const Controller = require('egg').Controller;

class FeedbackController extends Controller {
  // 列表
  async index(){
    const { ctx,app } = this;

    let tabs = [{
        name:"全部",
        url:"/admin/feedback",
        active:false
    },{
        name:"平台反馈",
        url:"?type=0",
        type:0,
        active:false
    },{
        name:"用户举报",
        url:"?type=1",
        type:1,
        active:false
    },{
        name:"帖子举报",
        url:"?type=2",
        type:2,
        active:false
    }]


    tabs = tabs.map(item=>{
        if( (!ctx.query.type && ctx.query.type != 0 && item.url === '/admin/feedback') || item.type == ctx.query.type){
            item.active = true
        }
        return item
    })

    let where = (!ctx.query.type && ctx.query.type != 0) ? {} : {
        type:ctx.query.type
    }

    let data = await ctx.page('Feedback',where,{
        include:[{
            model:app.model.User
        }]
    })

    data = JSON.parse(JSON.stringify(data))

    await ctx.renderTemplate({
        title:"举报/反馈列表",
        tempType: "table",
        table:{
            tabs,
            // 表头
            columns:[
                {
                title:"举报人",
                fixed: 'left',
                width: 180,
                render(item) {
                    let image = item.user.avatar ? item.user.avatar : '/public/assets/img/profiles/avatar-03.jpg'
                    return `
                    <h2 class="table-avatar">
                         <a href="profile.html" class="avatar avatar-sm mr-2"><img class="avatar-img rounded-circle" src="${image}"></a>
                         <a href="profile.html">${item.user.username}</a>
                    </h2>
                    `
                }
            },{
                title:"反馈对象id",
                fixed: 'center',
                width: 100,
                key:"reportedId"
            },{
                title:"反馈类型",
                fixed: 'center',
                width: 100,
                render(item){
                    if(item.type == 0){
                        let typeName="平台反馈"
                        return `
                        ${typeName}
                        `
                    }
                    else if(item.type == 1){
                        let typeName="用户举报"
                        return `
                        ${typeName}
                        `
                    }
                    else if(item.type == 2){
                        let typeName="帖子举报"
                        return `
                        ${typeName}
                        `
                    }
                }
            },{
                title:"创建时间",
                fixed: 'center',
                width: 180,
                key:"created_time"
            },{
                title:"反馈内容",
                fixed: 'center',
                width: 350,
                key:"content"
            },{
                title:"受理状态",
                fixed: 'center',
                width: 100,
                render(item){
                    if(item.check == 0){
                        let typeName="未受理"
                        return `
                        ${typeName}
                        `
                    }
                    else if(item.check == 1){
                        let typeName="已受理"
                        return `
                        ${typeName}
                        `
                    }
                }
            },{
                title: "操作",
                width: 200,
                fixed: 'center',
                render(item){
                    let check=''
                    if(item.check == 0){
                        check=`
                        <a class="btn btn-sm bg-success-light mr-2"
                        @click="modal('/admin/feedback/check/${item.id}','是否标记为已处理？')"><i class="fe fe-check"></i>
                        授权</a>
                        `
                    }
                    return `<div class="actions"> ${check}
                    <a class="btn btn-sm bg-danger-light" @click="del('/admin/feedback/delete/${item.id}')">
                    <i class="fe fe-trash"></i> 删除
                    </a>
                    </div>`
                }   
            }]
        },
        data
    })
}
  async check(){
    const { ctx,app }=this;
    let id = ctx.params.id

    let data = await app.model.Feedback.findOne({
        where:{
              id:id
        }
    })

    if(data.check==0){
        data.check=1;
        await data.save();
        ctx.toast('反馈已处理','success');
    }
    ctx.redirect('/admin/feedback')
  }

  async delete(){
    const { ctx,app }=this;
    let id = ctx.params.id

    await app.model.Feedback.destroy({
        where:{
              id:id
        }
    })
    ctx.toast('删除成功','success')
    ctx.redirect('/admin/feedback')

  }

}

module.exports = FeedbackController;
